//
//  OADepAddressController.m
//  OASystem
//
//  Created by zhoupengfei on 16/1/17.
//  Copyright © 2016年 zpf. All rights reserved.
//

#import "OADepAddressController.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "OAAddressDetailViewController.h"
#import "Toast+UIView.h"
#import "PersonViewController.h"
#import "nameTableViewCell.h"
@interface DepAddressCellList : UITableViewCell


@end

@implementation DepAddressCellList



@end

@interface OADepAddressController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation OADepAddressController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    return _contents;
}

- (void)viewDidLoad {
// 人员选择的控制器
    [super viewDidLoad];
    
      [self setRightItem];
      __weak typeof(self) weakSelf = self;
    if (!_isNeedRefresh) {
    self.currentPage = 1;
    [self requestData];

}else{
    self.currentPage = 1;

[self searchdata];
    


}
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
   UIImageView *backImageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
       [backImageView setImage:[UIImage imageNamed:@"通讯录背景"]];
self.tableView.backgroundView = backImageView;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];

        [self setRightItem];
        [self.tableView addHeaderWithCallback:^{
        if (_isNeedRefresh) {
    [weakSelf requestMoreData];
}else{
        [self.tableView headerEndRefreshing];
        }
                               }];
//
        [self.tableView addFooterWithCallback:^{
            if ((self.currentPage - 1)*KPageSize < self.totalNumber) {
                        if (weakSelf.isNeedRefresh) {

[self searchdata];

    
}else{
                 [weakSelf requestMoreData];
                 }
            }else{
                [weakSelf.view.window makeToast:@"没有更多数据了"];
                [weakSelf.tableView footerEndRefreshing];
            }
           
        }];
//    }

}
-(void)refreshData{
    self.currentPage = 1;
    [self requestData];
}
//加载更多
-(void)requestMoreData{
[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

        NSArray *  array = @[@{@"String":_string},@{@"userId":userId},@{@"page":[NSString stringWithFormat:@"%ld",(long)self.currentPage]}];;

 [HttpTools requestWithURL:@"exchother" arrays:array method:@"adressListSearch" success:^(id JSON) {
     

        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
                self.currentPage++;
 
        if (self.tableView.headerRefreshing) {
            [self.tableView headerEndRefreshing];
        }
        if (self.tableView.footerRefreshing) {
            [self.tableView footerEndRefreshing];
        }

    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];

    
    
}
-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}


-(void)searchAction{
    
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}
//搜素通讯录
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
      
    _string = textField.text;
    [self searchdata];
}
}
- (void)searchdata{
[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray *  array = @[@{@"String":_string},@{@"userId":userId},@{@"page":@"1"}];;
        [HttpTools requestWithURL:KExchother arrays:array method:@"adressListSearch" success:^(id JSON) {
       
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
            NSArray * tempArray = JSON[@"data"];
            NSDictionary *dict = [tempArray firstObject];
            _isNeedRefresh = YES;
                    self.contents = tempArray;
                [self.tableView footerEndRefreshing];

           [_tableView reloadData];

            
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        }];
    
}

-(void)requestData{
    
   // NSArray *  array =[NSArray arrayWithObjects:@{@"path":self.path}, nil];
        [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

      NSArray *  array = @[@{@"path":self.path}];
    
    
    
      [HttpTools requestWithURL:@"exchother" arrays:array method:@"BmRyTxlTree" success:^(id JSON) {
          

        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIdentifier = @"cellId";
            nameTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                cell.backgroundColor = [UIColor clearColor];

            if (cell == nil) {
                cell = [[nameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                    cell.backgroundColor = [UIColor clearColor];

            }
    NSDictionary *dict = self.contents[indexPath.row];
//    cell.title.text = dict[@"surname"];
    cell.dict = dict;
    cell.isNeedRefresh  = _isNeedRefresh;

    return cell;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

@end
